//
//  CBattleManager.cpp
//  Monster_Martial_Arts_Version_0
//
//  Created by chiawei.tseng on 13/1/4.
//
//

#include "CBattleManager.h"
#include "string.h"
#include "CMonsterData.h"
#include "CCArmatureFactory.h"

CBattleManager::CBattleManager()
{
    //m_Team[BATTLE_TEAM_RED].m_bIsRegister = false;
    //m_Team[BATTLE_TEAM_BLUE].m_bIsRegister = false;
    
    m_pBattleLayer = NULL;
}

CBattleManager::~CBattleManager()
{
    //Release();
}

void CBattleManager::Init()
{
    CMonsterDatas::Create();
    //CMonsterDatas::Get().LoadXMLData();
    CCArmatureFactory::Create();
    
    CCArmatureFactory::Get().Register("knight2.xml","knight2.png", "knight2.plist");
}

/*
void CBattleManager::Release()
{
    UnRegister(BATTLE_TEAM_RED);
    UnRegister(BATTLE_TEAM_BLUE);
}
*/

/*
void CBattleManager::Start()
{
    
}
*/

/*
void CBattleManager::Leaving()
{
    CleanAllTemMember(BATTLE_TEAM_RED);
    CleanAllTemMember(BATTLE_TEAM_BLUE);
}
*/

void CBattleManager::RegisterLayer(CCNode* pNode)
{
    m_pBattleLayer = pNode;
    
    m_pBattleLayer->retain();
}

void CBattleManager::UnRegisterLayer()
{
    m_pBattleLayer->release();
    
    m_pBattleLayer = NULL;
}

CCNode* CBattleManager::GetBattleLayer()
{
    return m_pBattleLayer;
}


TEAM& CBattleManager::GetTeam(BATTLE_TEAM_NUMBER battleTeamNumber)
{
    return m_Team[battleTeamNumber];
}

/*
void CBattleManager::RegisterTeam(BATTLE_TEAM_NUMBER battleTeamNumber,TEAM team)
{
    CCAssert(battleTeamNumber < BATTLE_TEAM_NUMBER_MAX, "battleTeamNumber >= BATTLE_TEAM_NUMBER_MAX");
    
    if (m_Team[battleTeamNumber].m_bIsRegister == false)
    {
        m_Team[battleTeamNumber] = team;
        
        m_Team[battleTeamNumber].m_bIsRegister = true;
    }
}

void CBattleManager::UnRegister(BATTLE_TEAM_NUMBER battleTeamNumber)
{
    CleanAllTemMember(battleTeamNumber);
    
    m_Team[battleTeamNumber].m_bIsRegister = false;
}
*/

void CBattleManager::AddTeamMember(BATTLE_TEAM_NUMBER battleTeamNumber, CBattleObject* obj)
{
    CCAssert(battleTeamNumber < BATTLE_TEAM_NUMBER_MAX, "battleTeamNumber >= BATTLE_TEAM_NUMBER_MAX");
    
    //CCAssert(m_Team[battleTeamNumber].m_TeamMember.count(TeamMemberID)<0 , "Team has this TeamMemberID");
    
    CCAssert(obj,"obj is NULL");
    
    //m_Team[battleTeamNumber].m_TeamMember[TeamMemberID] = obj;
    
    m_Team[battleTeamNumber].m_TeamMember.push_back(obj);
    
    obj->SetTeamNumber(battleTeamNumber);
    
}

void CBattleManager::CleanAllTemMember(BATTLE_TEAM_NUMBER battleTeamNumber)
{
    TeamMemberIter pi = m_Team[battleTeamNumber].m_TeamMember.begin();
    
    while (pi != m_Team[battleTeamNumber].m_TeamMember.end())
    {
        
        {
            (*pi)->Release(m_pBattleLayer);
        }
        
        pi = m_Team[battleTeamNumber].m_TeamMember.erase(pi);
    }
    
    //m_Team[battleTeamNumber].m_TeamMember.clear();
}

CBattleObject* CBattleManager::GetTeamMember(BATTLE_TEAM_NUMBER battleTeamNumber , int TeamMemberID)
{
    CCAssert(battleTeamNumber < BATTLE_TEAM_NUMBER_MAX, "battleTeamNumber >= BATTLE_TEAM_NUMBER_MAX");
    
    //CCAssert(m_Team[battleTeamNumber].m_TeamMember.count(TeamMemberID)>0 , "Team has no this TeamMemberID");
    
    return m_Team[battleTeamNumber].m_TeamMember[TeamMemberID];
}

